<template>
  <div class="rooms-page">
    <!-- 顶部搜索和筛选区 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="自习室名称" prop="roomName">
          <el-input
            v-model="queryParams.roomName"
            placeholder="请输入自习室名称"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input
            v-model="queryParams.location"
            placeholder="请输入位置"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
            <el-option label="营业中" :value="1" />
            <el-option label="维护中" :value="2" />
            <el-option label="暂停使用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备设施" prop="equipment">
          <el-select v-model="queryParams.equipment" placeholder="请选择设备" clearable size="small">
            <el-option label="空调" value="空调" />
            <el-option label="电源插座" value="电源插座" />
            <el-option label="WIFI" value="WIFI" />
            <el-option label="投影仪" value="投影仪" />
            <el-option label="白板" value="白板" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="getRooms">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8" style="margin-top: 20px;">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>

    <!-- 自习室列表 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="rooms"
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="roomName" label="自习室名称" />
        <el-table-column prop="location" label="位置" />
        <el-table-column prop="totalSeats" label="总座位数" width="100" align="center" />
        <el-table-column prop="equipment" label="设备设施" :show-overflow-tooltip="true" />
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="自习室名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入自习室名称" />
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="总座位数" prop="totalSeats">
          <el-input-number v-model="form.totalSeats" :min="1" :max="100" />
        </el-form-item>
        <el-form-item label="设备设施" prop="equipment">
          <el-input v-model="form.equipment" placeholder="请输入设备设施，用逗号分隔" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option label="营业中" :value="1" />
            <el-option label="维护中" :value="2" />
            <el-option label="暂停使用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="form.description" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  data() {
    return {
      rooms: [],
      loading: false,
      // 查询参数
      queryParams: {
        roomName: undefined,
        location: undefined,
        status: undefined,
        equipment: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        roomName: undefined,
        location: undefined,
        totalSeats: 20,
        equipment: undefined,
        status: 1,
        description: undefined
      },
      // 表单校验
      rules: {
        roomName: [
          { required: true, message: '请输入自习室名称', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入位置', trigger: 'blur' }
        ],
        totalSeats: [
          { required: true, message: '请输入总座位数', trigger: 'blur' }
        ]
      },
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: ''
    }
  },
  created() {
    this.getRooms()
  },
  methods: {
    getStatusType(status) {
      const typeMap = {
        0: 'info',
        1: 'success',
        2: 'warning'
      }
      return typeMap[status] || 'info'
    },
    getStatusText(status) {
      const statusMap = {
        0: '暂停使用',
        1: '营业中',
        2: '维护中'
      }
      return statusMap[status] || '未知状态'
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加自习室'
    },
    handleUpdate(row) {
      this.reset()
      this.form = { ...row }
      this.open = true
      this.title = '修改自习室'
    },
    handleDelete(row) {
      this.$confirm('是否确认删除该自习室?', '警告', {
        type: 'warning'
      }).then(() => {
        this.deleteRoom(row.id)
      })
    },
    reset() {
      this.form = {
        id: undefined,
        roomName: undefined,
        location: undefined,
        totalSeats: 20,
        equipment: undefined,
        status: 1,
        description: undefined
      }
    },
    resetQuery() {
      this.queryParams = {
        roomName: undefined,
        location: undefined,
        status: undefined,
        equipment: undefined
      }
      this.getRooms()
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            this.updateRoom(this.form)
          } else {
            this.addRoom(this.form)
          }
        }
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    // 获取自习室列表
    async getRooms() {
      this.loading = true
      try {
        const res = await request({
          url: '/api/study/studyroom/findAll',
          method: 'get'
        })
        if (res.code === 200) {
          this.rooms = res.data
        }
      } catch (error) {
        this.$message.error('获取自习室列表失败')
      }
      this.loading = false
    },
    
    // 添加自习室
    async addRoom(room) {
      try {
        const res = await request({
          url: '/api/study/studyroom/',
          method: 'post',
          data: room
        })
        if (res.code === 200) {
          this.$message.success('添加成功')
          this.open = false
          this.getRooms()
        }
      } catch (error) {
        this.$message.error('添加失败')
      }
    },
    
    // 更新自习室
    async updateRoom(room) {
      try {
        const res = await request({
          url: '/api/study/studyroom/',
          method: 'put',
          data: room
        })
        if (res.code === 200) {
          this.$message.success('更新成功')
          this.open = false
          this.getRooms()
        }
      } catch (error) {
        this.$message.error('更新失败')
      }
    },
    
    // 删除自习室
    async deleteRoom(id) {
      try {
        const res = await request({
          url: `/api/study/studyroom/${id}`,
          method: 'delete'
        })
        if (res.code === 200) {
          this.$message.success('删除成功')
          this.getRooms()
        }
      } catch (error) {
        this.$message.error('删除失败')
      }
    }
  }
}
</script>

<style scoped>
.rooms-page {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.search-section {
  margin-bottom: 30px;
}

.search-bar {
  position: relative;
  margin-bottom: 20px;
}

.search-bar input {
  width: 100%;
  padding: 15px 20px;
  padding-left: 50px;
  border: none;
  border-radius: 12px;
  background: white;
  font-size: 16px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.search-bar i {
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
  color: #909399;
  font-size: 20px;
}

.filters {
  display: flex;
  gap: 15px;
  overflow-x: auto;
  padding-bottom: 10px;
}

.filter-item {
  padding: 8px 20px;
  border-radius: 20px;
  background: white;
  cursor: pointer;
  transition: all 0.3s;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.filter-item.active {
  background: #409EFF;
  color: white;
}

.rooms-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.room-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.room-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0,0,0,0.15);
}

.room-image {
  height: 180px;
  background-size: cover;
  background-position: center;
  position: relative;
}

.room-status {
  position: absolute;
  top: 15px;
  right: 15px;
  padding: 6px 12px;
  border-radius: 20px;
  color: white;
  font-size: 12px;
  backdrop-filter: blur(10px);
}

.room-status.open {
  background: rgba(103, 194, 58, 0.9);
}

.room-status.closed {
  background: rgba(245, 108, 108, 0.9);
}

.room-status.maintain {
  background: rgba(230, 162, 60, 0.9);
}

.room-info {
  padding: 20px;
}

.room-info h3 {
  margin: 0 0 10px;
  font-size: 18px;
  color: #303133;
}

.room-meta {
  display: flex;
  gap: 15px;
  color: #606266;
  font-size: 14px;
  margin-bottom: 15px;
}

.room-meta span {
  display: flex;
  align-items: center;
  gap: 5px;
}

.room-features {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 15px;
}

.room-features span {
  padding: 4px 8px;
  background: #f5f7fa;
  border-radius: 4px;
  font-size: 12px;
  color: #606266;
  display: flex;
  align-items: center;
  gap: 4px;
}

.room-description {
  color: #909399;
  font-size: 14px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.room-footer {
  padding: 15px 20px;
  border-top: 1px solid #f0f2f5;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.room-rating {
  color: #F7BA2A;
  display: flex;
  align-items: center;
  gap: 5px;
}

.book-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 20px;
  background: #409EFF;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
}

.book-btn:hover {
  background: #66b1ff;
  transform: translateY(-2px);
}

.load-more {
  text-align: center;
  margin-top: 30px;
  padding: 15px;
  color: #409EFF;
  cursor: pointer;
}

@media (max-width: 768px) {
  .rooms-page {
    padding: 10px;
  }
  
  .rooms-grid {
    grid-template-columns: 1fr;
  }
  
  .room-image {
    height: 150px;
  }
}
</style> 